const { Category, Sequelize } = require('../models')
const { Op } = Sequelize
module.exports = [
  {
    /**
     * @api {post} /api/category/list [分类列表]
     * @apiGroup Category
     * @apiPermission none
     * @apiParam  {String} [name] 分类名称,模糊搜索
     * @apiParam  {Number} [offset=0]
     * @apiParam  {Number} [limit=10]
     */
    url: '/api/category/list',
    method: 'post',
    request: (req, res) => {
      const { offset, limit, name = null } = req.body
      let where = {}
      if (name) where.name = { [Op.like]: `%${name}%` }

      Category.findAndCountAll({
        where,
        offset: Number(offset) || 0,
        limit: Number(limit) || 10
      })
        .then(result => {
          res.json(result)
        })
        .catch(error => {
          res.json({
            msg: '参数错误',
            error
          })
        })
    }
  },
  {
    /**
     * @api {get} /api/category/:id [分类详情]
     * @apiGroup Category
     * @apiPermission none
     * @apiDescription category详情
     * @apiParam  {Number} id 分类id
     */
    url: '/api/category/:id',
    method: 'get',
    request: (req, res) => {
      Category.findOne({
        where: {
          id: req.params.id
        }
      }).then(result => {
        if (result) {
          res.json(result)
        } else {
          res.json({
            msg: '找不到您想要的'
          })
        }
      })
    }
  },
  {
    /**
     * @api {post} /api/category/add [新增分类]
     * @apiGroup Category
     * @apiPermission none
     * @apiParam  {String} name 分类名称
     * @apiParam  {Number} [index=9999] 分类优先级
     */
    url: '/api/category/add',
    method: 'post',
    request: (req, res) => {
      const { name } = req.body
      try {
        Category.findOrCreate({
          where: {
            name
          },
          defaults: req.body
        }).spread((category, created) => {
          if (created) {
            res.json({ msg: '创建成功' })
          } else {
            res.json({ msg: '该记录已存在' })
          }
        })
      } catch (error) {
        res.json({ msg: '创建失败', error })
      }
    }
  },
  {
    /**
     * @api {delete} /api/category/delete/:id [删除分类]
     * @apiGroup Category
     * @apiPermission none
     * @apiParam  {Number} id 分类id
     */
    url: '/api/category/delete/:id',
    method: 'delete',
    request: (req, res) => {
      const { id } = req.params
      Category.findOne({
        where: { id }
      }).then(category => {
        if (!category) return res.json({ msg: '找不到您想要的' })
        Category.destroy({ where: { id } })
          .then(results => {
            res.json({ msg: '删除成功' })
          })
          .catch(() => {
            res.json({ msg: '删除失败' })
          })
      })
    }
  },
  {
    /**
     * @api {put} /api/category/update [更新分类]
     * @apiGroup Category
     * @apiPermission none
     * @apiParam  {Number} id 分类id
     * @apiParam  {String} name 分类名称
     * @apiParam  {Number} [index] 分类优先级
     */
    url: '/api/category/update',
    method: 'put',
    request: (req, res) => {
      const { id } = req.body
      Category.findOne({ where: { id } }).then(category => {
        if (!category) return res.json({ msg: '没有该记录' })
        Category.update(req.body, { where: { id } })
          .then(results => {
            res.json({ msg: '更新成功' })
          })
          .catch(err => {
            res.json({ msg: '更新失败', err })
          })
      })
    }
  }
]
